package com.zy.cat.common.utils;

import java.util.HashMap;
import java.util.Map;

import javax.management.Query;

import org.apache.commons.lang3.RandomUtils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayUserCertifyOpenCertifyRequest;
import com.alipay.api.request.AlipayUserCertifyOpenInitializeRequest;
import com.alipay.api.request.AlipayUserCertifyOpenQueryRequest;
import com.alipay.api.response.AlipayUserCertifyOpenCertifyResponse;
import com.alipay.api.response.AlipayUserCertifyOpenInitializeResponse;
import com.alipay.api.response.AlipayUserCertifyOpenQueryResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyResultRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyResultResponse;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyTokenRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyTokenResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

import lombok.extern.slf4j.Slf4j;

/**
 * 淘宝接口
 * 
 * @author Link
 * @date 2019-06-27 11:43
 */
@Slf4j
public class AliZMAPI {

	/**
	 * 服务地址，目前为沙盒地址 正式地址:https://openapi.alipay.com/gateway.do 沙盒地址
	 * ：https://openapi.alipaydev.com/gateway.do
	 */
	private String serverUrl = "https://openapi.alipay.com/gateway.do";

	/** 商户appid */
	private String appId = "2016101500692369";// 正式商户id:2017121200620796
	/** 公钥 */
	private String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAksD6nGqDRw7F9cFENtP2oTcOLj2XWYqOJ9nV+aUAJXmBOsSA6lPLOm21/D1ha18E+1vUjtg96Ngof8eB6XGziLlNf6xkwnwjgABCK0iKpBAZ4TLzaoZpmVK/RdMxgT7Xe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8GFA5p5akH+EGIGitUuVH9EJRWDb6WnNCiJ18wiJ7ohC4ioYJTaky0zF3SZh6CUSuigVTA4lHLtIzMCgYEA0I7t7FSCPBJEQYLjIOfMuca1CvMKV60RCg5YDeVUSI6kZZa+xuIN2RRMDj6UGZSJChskl+1Z08j1mCwBNLkqBu2mO4zbo1wA+Iq/1DPLWaVqpsF+jLiaf6XS5kPIEEffCgBUj0TVjhdO+hQnyF8zxein4orsV5G4pXIR+OSEFD8CgYAibG6tGWDkJzzB6k0wG8hsYS7s4M7yKEjmrYJzykk8NwYIVAmLMnqjOEiQ/CPKB4rRiqV4yityDSSr/HdKBO91FExNByZaA/wmioWswXgh4IdSgZYgy24mriUd52js0DhBf/joXAkwV3pzNZJ4oMz+G7KDH3rP+xqRElwt2XiDsQKBgQCCyT72t0A8f5ACLRxV5EcEMVITHbpcpRFMZxZdEHU8UwdRFalpexUIDqxrOl8Rc45EoiTeiV+NajZpSR8wevWaBqn6KikKVoXdVtVZM2nQO+WX0TuU76cjaU/KSrNJUmjSEsb0WUw6oO9L8BvKkbkb4Px3Gnt/pshOKCyREmypbQKBgQCGWqFbR2jN/mazNhn1BWSxlZSiwl9/Rx93BGKAPZdaiPuvJ541JOSKbHlMXR/2sQwcK3TY7Ksy++Gqi0/mITu+drSqaNCQ4pl8PQwj/KhheMr6NmuipijvoljGA3aDtaswt3GyZAZfsVj2FQ0tBbzTEe3QaETk+ewulQf1xgNcRQ==";

	/** 返回格式 */
	private static final String format = "json";
	/** 编码格式 */
	private static final String charset = "UTF-8";
	/** 返回路径 */
	private static final String return_url = "haimai://identity:8888/identitySuccess";
	/** 功能--face 人脸检测 */
	private static final String biz_code_face = "CERT_PHOTO_FACE";

	private AliZMAPI(String appId, String ALIPAY_PUBLIC_KEY, String private_key) {
		super();
		this.appId = appId;
		this.ALIPAY_PUBLIC_KEY = ALIPAY_PUBLIC_KEY;
		this.private_key = private_key;
	}

	/**
	 * 获取不同账户的api
	 * 
	 * @author Link
	 * @date 2019-06-27 14:01
	 * @param type 0.九趣猫账户
	 * @return
	 */
	public static AliZMAPI getInstance(int type) {

		String appId = "2021001102605553";
		/** 公钥 */
		String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0ccl3DopRaWZoZ5GNnLWVbm6RUjT8zxqRA+7ukpEGu4UCKoeZYe0iKevGeWZyeanufCvPMSifFkO2utZa7ofrYLqleqIEhl2SYP9DkrC6cC950uN7ZWEQqfqRRN00YzL4eysKWaF0Zyi/OkJJtQ+VSUuRLg24NDw6KzMqpU9bCFolrb+GaZiFXlMKecBb+P9NlPHHMnSZ7iFM29SZP9y3mD4kz4koXjZx45XQtlbgxqsY1ChYgv+pfAJM/r55RXcGL/tdC+0zy1i2MxkDF+LL/hf4pvi6XDMzzPAXWoj5pgCfweeGbaflpDILAXWZmZ9+V6+nJgidjbk0OULXMtPPQIDAQAB";
		/** 私钥 */
		String private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCNuQN+lTJ+HEwfZVfvCxSjnQ23vfJJsDi3bHO877hQN5d9t+XT3fbF/VaLnefSBEoMc1H3HZuNOWBZSTvIoo3IXQZEtMVwfKS3z1j7rKqwPNbVVu0OpjzqMD+tljTXNilZqtYFOlyB5FZodrU/Zm0vNe97Kh9Y8bIu753uFSeetvneXjQh1IkpwK1gz4n638f7oZquUFvIyIjY8hBHlBh6b7mi7i1ejJQr/IY8/gs3Esm2wiSEPQeCDdoRSlUS7p3aITgl2qlPyxf02T0YpanMlJ/MjHHRtb/d/5++zG07PtTvoAGs/0etrLLO7ZU3DyAceOdR5F9nTVpWxO3sfqN1AgMBAAECggEAZEFCSJSQsIeiDf+qEe8bvZZHcvjK4ffoV1FAGqLWFuEMqKGj0pnNjUfmpkOhD397Ro15qT9nn6K8Fs1onkjn1ZqA6a+bkDSMbX8fqoH+CUgwjaC9d8R/iQdytIByht/IpqbkWbRsZnK58ZIwK1Zw3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x//eEp/XYYpooqO3M=";
//		if (type == 1) {
//			serverUrl = "https://openapi.alipay.com/gateway.do";
//			/** 商户appid */
//			appId = "2017121200620796";
//			/** 公钥 */
//			ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAksD6nGqDRw7F9cFENtP2oTcOLj2XWYqOJ9nV+aUAJXmBOsSA6lPLOm21/D1ha18E+1vUjtg96Ngof8eB6XGziLlNf6xkwnwjgABCK0iKpBAZ4TLzaoZpmVK/RdMxgT7XeyjxrYADkP+Erv0C4kKZoDohHp8bT0sMRbA1rTQnA8KM6B6Z8oCAX0iAEP2y0VSsl/BRJPi3o7vfIukI/YybV2pmMIQUNfrhtBR6Rlp++eYQhgBZcRDNwcMv2KgLgRlOdpQslaWWqJrh7DFNO76XfWpntItK31Ol1gaiW01ogAc6uJbmTHqPcakc0iAseHPfhT1WZDShjNhVLzMJP+nLXwIDAQAB";
//			/** 私钥 */
//			private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC1U83HInWSH5pqzW2roXjefoF5Umoy1hB+gYl7BY0oNNPrO7L1keR8TFntQgyqS8xl9L5fwNVpABc0Cx/o+SMkapmlhOcnbHvJEd0U+G/Sp1iaXOBYBfGXuLB58n7y6ObyxlGU9XC3rVQ/MtGnpoishqydzHbs5s6NEK1JGQ3953kj0f0tT3fxbzTWgSPQoqjmcG97ual6+lqaA83SMrEqa9lrOgtHM20SFgdbTV6QtFOzuQb6snWR6+OxTBlhW603vqQGEXm+xtzESsBAIA9i6k+LKoHpP1JuK14xwWLRKuHxgwINrszu8uFCb23cuuZN2Yknl4OFwL72d4phZ6WNAgMBAAECggEBAIighavnThl1UJ/q/BjXiUB0p9AnhjeZzkHUDygwDaVe8NMNX5HjhPsXeKbqHqruhCvKluuFXAhK9jDTtQ6RMqaM4Aq0soulFTxzBld3vqJGT9+33Z7Vk61Tjz3Dt14GmYYSYoRPz8fGhXU9M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";
//		}
		return new AliZMAPI(appId, ALIPAY_PUBLIC_KEY, private_key);
	}

	public String alipayUserCertif(String cert_name, String cert_no) {
		AlipayClient alipayClient = new DefaultAlipayClient(this.serverUrl, this.appId, this.private_key, format,
				charset, this.ALIPAY_PUBLIC_KEY, "RSA2");
		AlipayUserCertifyOpenInitializeRequest request = new AlipayUserCertifyOpenInitializeRequest();

		Map<String, Object> bizMap = new HashMap<String, Object>();
		Map<String, String> identity_param = new HashMap<String, String>();
		String outer_order_no = "JQMALRZ" + RandomUtils.nextLong(1000000, 10000000);
		bizMap.put("outer_order_no", outer_order_no);
		bizMap.put("biz_code", biz_code_face);

		identity_param.put("identity_type", "CERT_INFO");
		identity_param.put("cert_type", "IDENTITY_CARD");
		identity_param.put("cert_name", cert_name);
		identity_param.put("cert_no", cert_no);
		bizMap.put("identity_param", identity_param);
		Map<String, String> merchant_config = new HashMap<String, String>();
		merchant_config.put("return_url", "https://wechat.taojingke.cn/wechat/");
		bizMap.put("merchant_config", merchant_config);

		String bizContent = JSON.toJSONString(bizMap);
		log.info("--->" + JSON.toJSONString(identity_param));
		request.setBizContent(bizContent);

		AlipayUserCertifyOpenInitializeResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}

		log.info("-response->" + response.getBody());
		if (response.isSuccess()) {
			System.out.println("调用成功");

			AlipayUserCertifyOpenCertifyRequest arequest = new AlipayUserCertifyOpenCertifyRequest();
			Map<String, String> certify_id = new HashMap<String, String>();
			certify_id.put("certify_id", response.getCertifyId());
			request.setBizContent(JSON.toJSONString(certify_id));
			AlipayUserCertifyOpenCertifyResponse aresponse = null;
			try {
//				aresponse = alipayClient.pageExecute(arequest);
				aresponse = alipayClient.pageExecute(arequest, "GET");
			} catch (AlipayApiException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (response.isSuccess()) {
				System.out.println("调用成功-->" + aresponse.getBody());
			} else {
				System.out.println("调用失败");
			}

		} else {
			System.out.println("调用失败");
		}
		return response.getBody();
	}
	
	public void query(String id) {
		AlipayClient alipayClient = new DefaultAlipayClient(this.serverUrl, this.appId, this.private_key, format,
				charset, this.ALIPAY_PUBLIC_KEY, "RSA2");		
		AlipayUserCertifyOpenQueryRequest request = new AlipayUserCertifyOpenQueryRequest();
		request.setBizContent("{" +
		"\"certify_id\":\"OC201809253000000393900404029253\"" +
		"  }");
		AlipayUserCertifyOpenQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		System.out.println("调用-->"+response.getBody());
		if(response.isSuccess()){
		System.out.println("调用成功-->");
		} else {
		System.out.println("调用失败");
		}
	}

	@SuppressWarnings("deprecation")
	private void HRRZ() {
		DefaultProfile profile = DefaultProfile.getProfile("cn-jinan", // 您的可用区ID
				"LTAI4Fc3xE4kzbfAuQ5Sc4oj", // 您的Access Key ID
				"NB1WtwGDrMqzasax1W7axQpvVrbrUa"); // 您的Access Key Secret
		try {
			DefaultProfile.addEndpoint("cn-jinan", "cn-jinan", "Cloudauth", "cloudauth.aliyuncs.com");
		} catch (ClientException e) {
			e.printStackTrace();
		}
		IAcsClient client = new DefaultAcsClient(profile);

		DescribeVerifyTokenRequest request = new DescribeVerifyTokenRequest();
		
		String bizId = "JQMALRZ" + RandomUtils.nextLong(1000000, 10000000);
		
		request.setBizId(bizId);
		request.setBizType("JQMTXSRRZ"); // 创建方法请参见https://help.aliyun.com/document_detail/127885.htm

		DescribeVerifyTokenResponse response = null;
		try {
			response = client.getAcsResponse(request);
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String verifyToken = response.getVerifyToken();
		log.info("-verifyToken-response->"+JSON.toJSONString(response));
		log.info("-verifyToken-->"+verifyToken);

//2. 接入方服务端将token传递给接入方无线客户端
//3. 接入方无线客户端用token调起无线认证SDK
//4. 用户按照由无线认证SDK组织的认证流程页面的指引，提交认证资料
//5. 认证流程结束退出无线认证SDK，进入客户端回调函数
//6. 接入方服务端获取认证状态和认证资料（注：客户端无线认证SDK回调中也会携带认证状态, 但建议以服务端调接口获取的为准进行业务上的判断和处理）
		DescribeVerifyResultRequest verifyResultRequest = new DescribeVerifyResultRequest();
		verifyResultRequest.setBizId(bizId);
		verifyResultRequest.setBizType("JQMTXSRRZ");
		try {
			DescribeVerifyResultResponse verifyResultResponse = client.getAcsResponse(verifyResultRequest);
			
			log.info("-verifyResultResponse-->"+JSON.toJSONString(verifyResultResponse));
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		String cert_name = "李云涛";
		String cert_no = "372928199111106310";
		// 1604e323d6b3e765a35792ec1b6b04ed
//		AliZMAPI.getInstance(0).query("21c3b7df30dd5b5567186c1ead010b90");
		
		AliZMAPI.getInstance(0).HRRZ();

//		AliZMAPI.getInstance(0).alipayUserCertif(cert_name, cert_no);
		
	}

}
